package user;

import java.util.List;

import com.atlassian.crowd.embedded.api.SearchRestriction;
import com.atlassian.crowd.exception.ApplicationPermissionException;
import com.atlassian.crowd.exception.GroupNotFoundException;
import com.atlassian.crowd.exception.InvalidAuthenticationException;
import com.atlassian.crowd.exception.OperationFailedException;
import com.atlassian.crowd.integration.rest.service.factory.RestCrowdClientFactory;
import com.atlassian.crowd.model.group.Group;
import com.atlassian.crowd.search.query.entity.EntityQuery;
import com.atlassian.crowd.search.query.entity.restriction.MatchMode;
import com.atlassian.crowd.search.query.entity.restriction.TermRestriction;
import com.atlassian.crowd.search.query.entity.restriction.constants.UserTermKeys;
import com.atlassian.crowd.service.client.CrowdClient;

public class RetrieveGroupInfo {

	public int maxResults = EntityQuery.MAX_MAX_RESULTS;
	public int startIndex = 0;
	String appLink = "https://code.bestbuy.com/crowd/";
	CrowdClient groupConn = new RestCrowdClientFactory().newInstance(appLink,"profile-mgt","1nitialPa55");



	public Group getGroup(String grpN) throws GroupNotFoundException, OperationFailedException, InvalidAuthenticationException, ApplicationPermissionException{
		Group grp = groupConn.getGroup(grpN); 
		System.out.println(grp);
		return grp;
	}

	public List searchGroups(String grpSt) throws OperationFailedException, InvalidAuthenticationException, ApplicationPermissionException{
		TermRestriction sterm = new TermRestriction (UserTermKeys.USERNAME,MatchMode.CONTAINS, grpSt); 
		SearchRestriction searchTerm = sterm;
		List<Group> searchGroupResults = groupConn.searchGroups(sterm, 0, EntityQuery.MAX_MAX_RESULTS);

		for (int i=0; i<searchGroupResults.size(); i++){
			System.out.print(searchGroupResults.get(i));
		}


		return searchGroupResults;

	}


	public CrowdClient getClient(){
		return groupConn; 
	}


}
